package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 86182
 * @Date: 2021/8/31
 * @Time: 18:52
 * @Version:
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        page = page == 0 ? 1 : page;
        Integer offSet = (page - 1) * rows;
        List<Supplier> supplierList = this.supplierDao.list(offSet,rows,supplierName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",supplierList.size());
        map.put("rows",supplierList);
        return map;
    }

    @Override
    public void save(Supplier supplier,Integer supplierId) {
        if (supplierId == null){
            this.supplierDao.save(supplier);
        }else {
            this.supplierDao.update(supplier, supplierId);
        }
    }

    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");
        for (String id : split) {
            this.supplierDao.delete(Integer.parseInt(id));
        }
    }
}
